#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

const char *exe = "otherExe";

int main()
{
    extern char **environ;//声明
    //给调用进程添加环境变量-添加到调用进程的上下文

    putenv("PRIVATE_ENV=6666666");

    pid_t id = fork();

    if(id == 0)
    {

      //  //定义数组 - 指针本身不能被改变(指向内容可改)
      //  char *const myargv[] = {
      //      "ls",
      //      "-l",
      //      "-a",
      //      NULL
      //      };

      //  //
      //  printf("process, pid: %d,ppid: %d\n",getpid(),getpid());

      //  //这类方法的标准写法
      //  //execl("/usr/bin/ls","ls","-a","-l",NULL);//程序替换-成功执行无返回值
      //  execlp("ls","ls","-a","-l",NULL);//环境变量里PATH下找文件"ls",执行

      //  execv("/usr/bin/ls",myargv);

      //  //对系统命令/别的程序进行封装，被我们的程序代替

      //  //execl后的代码没执行
      
      //用一个可执行程序去调另一个可执行程序
        
        char* const myargv[]={
            "otherExe",
            "-a",
            "-b",
            "-c",
            NULL
            };
        
        char *const myenv[] = { "MYVAL=77777","MYPATH=/bin/999",NULL};
        
        execle("./otherExe","otherExe","-a","-b","-c",NULL,myenv);//传自定义环境变量

        execle("./otherExe","otherExe","-a","-b","-c",NULL,environ);//可传环境变量

        execv("./otherExe",myargv);
        //execl("./otherExe","otherExe",NULL);//第一个只路径，在哪里--当前文件下otherExe程序
        //execl("/usr/bin/python3","python3","test.py",NULL);//脚本语言，php，java.....都可以用execl运行起来
        printf("process, pid: %d,ppid: %d\n",getpid(),getpid());

        exit(0);
    }

    //parent
    pid_t ret = waitpid(id,NULL,0);
    if(ret > 0)
        printf("wait success,pid: %d ,ret id: %d \n",getpid(),ret);
    return 0;
}
